﻿using EPiServer.Community.Blog;
using JoelAbrahamsson.TestUtilities;
using Moq;
using StructureMap.AutoMocking;
using Xunit;

namespace JoelAbrahamsson.MetaWeblog.Tests
{
    public class GetCategoriesTests
    {
        [Fact]
        public void GivenBlogIDAndCredentials_GetCategories_ValidatesUser()
        {
            int blogID = 1;
            string username = RandomValues.String;
            string password = RandomValues.String;
            Mock<Blog> fakeBlog = new Mock<Blog>();
            fakeBlog.Setup(blog => blog.ID).Returns(blogID);
            MoqAutoMocker<MetaWeblogHandler> metaWebLogHandlerAutoMocker = new MoqAutoMocker<MetaWeblogHandler>();
            metaWebLogHandlerAutoMocker.PartialMockTheClassUnderTest();
            Mock<MetaWeblogHandler> fakeMetaWebLogHandler = Mock.Get(metaWebLogHandlerAutoMocker.ClassUnderTest);
            fakeMetaWebLogHandler.Setup(handler => handler.GetValidatedUser(It.IsAny<Blog>(), It.IsAny<string>(), It.IsAny<string>()));
            fakeMetaWebLogHandler.Setup(handler => handler.GetBlog(blogID.ToString())).Returns(fakeBlog.Object);
            
            metaWebLogHandlerAutoMocker.ClassUnderTest.GetCategories(blogID.ToString(), username, password);

            Mock.Get(metaWebLogHandlerAutoMocker.ClassUnderTest).Verify(handler => handler.GetValidatedUser(fakeBlog.Object, username, password));
        }

        [Fact]
        public void GivenBlogID_GetCategories_ReturnsCategoriesFromBlogCategoryProvider()
        {
            int blogID = 1;
            string username = RandomValues.String;
            string password = RandomValues.String;
            Mock<Blog> fakeBlog = new Mock<Blog>();
            fakeBlog.Setup(blog => blog.ID).Returns(blogID);
            MoqAutoMocker<MetaWeblogHandler> metaWebLogHandlerAutoMocker = new MoqAutoMocker<MetaWeblogHandler>();
            metaWebLogHandlerAutoMocker.PartialMockTheClassUnderTest();
            Mock<MetaWeblogHandler> fakeMetaWebLogHandler = Mock.Get(metaWebLogHandlerAutoMocker.ClassUnderTest);
            fakeMetaWebLogHandler.Setup(handler => handler.GetValidatedUser(It.IsAny<Blog>(), It.IsAny<string>(), It.IsAny<string>()));
            fakeMetaWebLogHandler.Setup(handler => handler.GetBlog(blogID.ToString())).Returns(fakeBlog.Object);
            Mock<IBlogCategoryProvider> fakeBlogCategoryProvider = Mock.Get(metaWebLogHandlerAutoMocker.Get<IBlogCategoryProvider>());
            CategoryInfo[] categoriesToReturn = new [] { new CategoryInfo() };
            fakeBlogCategoryProvider.Setup(provider => provider.GetBlogCategories(fakeBlog.Object)).Returns(categoriesToReturn);

            CategoryInfo[] returnedCategories = metaWebLogHandlerAutoMocker.ClassUnderTest.GetCategories(blogID.ToString(), username, password);

            Assert.NotNull(returnedCategories);
            foreach (CategoryInfo category in categoriesToReturn)
            {
                Assert.Contains(category, returnedCategories);
            }
        }
    }
}
